package com.store;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.model.Product;

public class StoreH2Jdbc implements DataStore {

	private Connection conn = null;
	//private Statement stat = null;
	private PreparedStatement prep = null;
	
	@Override
	public void InsertProduct(Product pr) {
		
		try {
			prep.setString(1, pr.getDescriere());
			//prep.setDouble(2, pr.getPret());
			prep.execute();
			prep.clearParameters();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void close() {
		
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void open(String db) {
			
		
		try {
			Class.forName("org.h2.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 try {
			conn = DriverManager.getConnection("jdbc:h2:tcp://localhost/"+db, "", "");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//prepare insert statament
		try {
			prep = conn.prepareStatement(DataStore.insertStr);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
